<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class control_user extends base_control{

	//hook ctr_user_start.php
	
	public function __construct() {
		//hook ctr_user_init_init_before.php
		parent::__construct();
		
		//hook ctr_user_init_init_after.php
		$this->tpl->assign('_site_sub_keywords', '个人中心');
		$this->tpl->assign('_site_sub_description', '个人中心');
		$this->tpl->assign('_site_sub_title', '个人中心');
		//hook ctr_user_init_end.php
	}

	public function do_register() {
		$rphone = core::gpc('rphone', 'P');
		$remail = core::gpc('remail', 'P');
		$rname = misc::utf8_sub_str(misc::safe_str(core::gpc('rname', 'P')),0,20);
		$rpass = core::gpc('rpass', 'P');
		$cof_rpass = core::gpc('cof_rpass', 'P');
		
		//hook ctr_user_register_start.php
		$reg_msg = $this->ckeck_register( $rphone, $remail,
								$rname, $rpass, $cof_rpass);
		$conf = $this->conf->get_conf_arr(5);
		
		//hook ctr_user_register_end.php
		$this->tpl->assign('notice_register', $conf['regnotice']);
		$this->tpl->assign('reg_msg', $reg_msg);
		$this->tpl->assign('rname', $rname);
		$this->tpl->assign('rpass', $rpass);
		$this->tpl->assign('rphone', $rphone);
		$this->tpl->assign('remail', $remail);
		$this->tpl->display('user_register.tpl');
	}
	
	public function do_login() {
		$uname = core::gpc('uname', 'P');
		$upass = core::gpc('upass', 'P');
		//hook ctr_user_login_start.php
		if(!empty($uname) && !empty($upass))
		{
			//hook ctr_user_login_before_make_ssid.php
			$ssid = $this->user->make_ssid($uname ,$upass);
			$this->this_user = $this->user->get_user_by_ssid($ssid);
			//hook ctr_user_login_before_is_logined.php
			$logined = $this->this_user->is_logined();
			if($logined){
				//hook ctr_user_login_success.php
				html::setcookie(CONFIG_COOKIE_PRE .'ssid', $ssid, time()+CONFIG_COOKIE_TIME,'/');
				$this->message('登录成功','index.php');
			}else{
				//hook ctr_user_login_failed.php
				$this->message('登录失败','index.php?user-login.htm');
			}
		}
		//hook ctr_user_login_end.php
		$this->tpl->display('user_login.tpl');
	}
	
	public function do_logout() {
		//hook ctr_user_do_logout.php
		html::setcookie(CONFIG_COOKIE_PRE .'ssid', '', time()-CONFIG_COOKIE_TIME,'/');
		$this->message('退出登录','index.php');
	}
	
	//=============>need login
	public function do_index() {
		//hook ctr_user_do_index.php
		$this->do_zone();
	}
	
	public function do_editphem() {
		$this->check_user_login_msg();
		//hook ctr_user_editphem_start.php
		$user_base = $this->this_user->base_info();
		
		$uemail = core::gpc('uemail', 'P');
		$uphone = core::gpc('uphone', 'P');
		
		$err_msg = '';
		$empt_ph = empty($uphone);
		$empt_em = empty($uemail);
		if(!$empt_ph || !$empt_em) {
			if(!$empt_ph) {
				//hook ctr_user_editphem_phone_before.php
				$err_msg = $this->check_phone($uphone) ? $err_msg :'手机号码格式不正确';
			}else{
				$uphone = '';
			}
			if(!$empt_em) {
				//hook ctr_user_editphem_email_before.php
				$err_msg = $this->check_email($uemail) ? $err_msg :'邮箱格式不正确';
			}else{
				$uemail = '';
			}
			if($err_msg == '') {
				$uid = $this->this_user->user_uid();
				//hook ctr_user_editphem_edit_before.php
				if($this->user->edit_user($uid, -1, -1, $uphone, $uemail , '','',''))
				{
					$err_msg = '设置成功';
					$user_base['email'] = $uemail;
					$user_base['phone'] = $uphone;
				}else{
					$err_msg = '设置失败';
				}
			}
		}
		
		//hook ctr_user_editphem_end.php
		
		$this->tpl->assign('edit_msg', $err_msg);
		$this->tpl->assign('uemail', $user_base['email']);
		$this->tpl->assign('uphone', $user_base['phone']);
		$this->tpl->display('user_editphem.tpl');
	}
	
	public function do_edithpho() {
		$this->check_user_login_msg();
		$alert_msg = '图片大小不能超过'. misc::humansize($this->confs['_file_allow_size']).'<br/>分辨率不能超过1000*1000';
		
		//hook ctr_user_edithpho_start.php
		if(html::form_submit( CONFIG_CS_KEY )) {
			//hook ctr_user_edithpho_before_upload.php
			$this->upimg->upload();
			if($this->upimg->error_code == 0) {
				//hook ctr_user_edithpho_upload_no_err.php
				$image_size = getimagesize($this->upimg->upload_path);
				if($image_size == false) {
					@unlink($this->upimg->upload_path);
					$this->message('图像格式及签名不正确', 'index.php?user-edithpho.htm');
				}
				if($image_size[0]>1000 || $image_size[1]>1000) {
					@unlink($this->upimg->upload_path);
					$this->message('分辨率不能超过1000*1000', 'index.php?user-edithpho.htm');
				}
				
				$uid = $this->this_user->user_uid();
				$user_base = $this->this_user->base_info();
				
				if($user_base['headphoto'] != '' && 
					file_exists(IN_APP.$user_base['headphoto'])) {
						@unlink(IN_APP.$user_base['headphoto']);
				}
				
				//hook ctr_user_edithpho_before_resize.php
				$this->rzimg->resize_img($this->upimg->upload_path, 100, 100, 1,
									CONFIG_HPO_PATH, $uid);
				@unlink($this->upimg->upload_path);
				if(file_exists($this->rzimg->result_img)) {
					//hook ctr_user_edithpho_before_edit.php
					if($this->user->edit_user_headphoto($uid, $this->rzimg->result_img)) {
						$this->message('修改成功', 'index.php?user-edithpho.htm');
					}else{
						$alert_msg = '修改失败';
					}
				}
			}else{
				//hook ctr_user_edithpho_before_get_err.php
				$alert_msg = $this->miscfunc->check_upload_err($this->upimg->error_code);
			}
		}
		$FORM_HASH = html::form_hash( CONFIG_CS_KEY );
		
		//hook ctr_user_edithpho_end.php
		$this->tpl->assign('FORM_HASH', $FORM_HASH);
		$this->tpl->assign('alert_msg', $alert_msg);
		$this->tpl->display('user_edithpho.tpl');
	}
	
	public function do_editpass() {
		$this->check_user_login_msg();
		$cpass = core::gpc('confpass', 'P');
		$upass = core::gpc('upass', 'P');
		
		$edit_msg = '';
		//hook ctr_user_editpass_start.php
		if(!empty($cpass) && !empty($upass)) {
			if($cpass != $upass) {
				$edit_msg = '重复密码错误';
			}else{
				$arr = $this->this_user->user_core();
				//hook ctr_user_editpass_before.php
				if($this->user->edit_user($arr['uid'], -1, -1,
						'', '', $upass, $arr['name'], '')) {
					//hook ctr_user_editpass_after.php
					$this->message('修改成功，请重新登录', 'index.php?user-login.htm');
				}else{
					$edit_msg = '修改失败';
				}
			}
		}
		//hook ctr_user_editpass_end.php
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->display('user_editpass.tpl');
	}
	
	public function do_zone() {
		$this->check_user_login_msg();
		
		$atmsg_one = array();
		if(isset($this->tpl->_tpl_vars['_user_atmsg_count']) &&
			$this->tpl->_tpl_vars['_user_atmsg_count'] > 0 )
		{
			//hook ctr_user_zone_atmsg_before.php
			$atmsg_one = $this->atmsg->atmsg_show($this->tpl->_tpl_vars['_user_uid']);
			if(!empty($atmsg_one))
				$this->atmsg->atmsg_edit($atmsg_one['mid']);
			//hook ctr_user_zone_atmsg_end.php
		}
		
		//称号
		$utitle_arr = $this->conf->get_conf_arr(6);
		$utitle_level = $this->miscfunc->get_level_core($this->tpl->_tpl_vars['_user_core']);
		$this->miscfunc->get_level_limit($utitle_level, $utitle_arr);
		$utitle_title = $utitle_arr[$utitle_level];
		
		//hook ctr_user_zone_end.php
		$this->tpl->assign('atmsg_one', $atmsg_one);
		$this->tpl->assign('utitle_title', $utitle_title);	
		$this->tpl->display('user_zone.tpl');
	}
	//==================>private
	private function ckeck_register($rphone, $remail,
								$rname, $rpass, $cof_rpass) {
		if($this->confs['_conf_reglock'] == 1) 
			$this->message('站点关闭注册，抱歉^_^', 'index.php');
			
		if(empty($rname) || empty($rpass)) {
			return '*号部分请填写完整';
		}
		if(empty($cof_rpass)) {
			return '请重复填写以确认密码';
		}
		if($cof_rpass != $rpass) {
			return '重复密码错误';
		}
		if(!empty($rphone) && !$this->check_phone($rphone)) {
			return '手机号码格式错误';
		}
		if(!empty($remail) && !$this->check_email($remail)) {
			return '邮箱格式错误';
		}
		$tusr = $this->user->get_user_by_name($rname, false);
		if(!empty($tusr)) {
			return '用户名已被占用';
		}
		//hook ctr_user_ckeck_register_before.php
		$ressid = $this->user->reg_user($rname, $rphone ,$remail,
									$rpass , $_SERVER['ip'], array() );
		if($ressid != false) {
			//hook ctr_user_ckeck_register_success.php
			html::setcookie(CONFIG_COOKIE_PRE .'ssid', $ressid, time()+CONFIG_COOKIE_TIME,'/');
			$this->message('注册成功', 'index.php');
		}
		return '注册失败';
	}
	
	private function check_phone($text = '') {
		$search ='/^(1(([35][0-9])|(47)|[8][0126789]))\d{8}$/';
		if(preg_match($search, $text)) {
			return true;
		}else {
			return false;
		}
	}
	
	private function check_email($remail = '') {
		$s = '/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i';
		if(preg_match($s, $remail )){
			return true;
		}else{
			return false;
		}
	}
	
	//hook ctr_user_end.php

}
/*{TranslateTagEnd}*/

?>